package juc.c_021_01Interview;
/*
   Talk is cheap. Show me the code.
 */

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Date 2021/6/20
 * @Author liudl
 */
public class T02_WithVolatile {
        volatile List lists = new ArrayList();
//    volatile List lists = Collections.synchronizedList(new LinkedList<>());
    public boolean add(Object o) {
        return lists.add(o);
    }

    public int size() {
        return lists.size();
    }

    public static void main(String[] args) {
        T02_WithVolatile t = new T02_WithVolatile();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                t.add(i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(i);
            }
        },"t1").start();

        new Thread(()->{
            while (true) {
                if (t.size() == 5) {
                    break;
                }
            }
            System.out.println("t2结束");
        },"t2").start();
    }
}
